package com.fastjrun.eladmin.itrms.service.impl;

import com.fastjrun.eladmin.itrms.common.exception.ServiceException;
import com.fastjrun.eladmin.itrms.dao.RmsServerDao;
import com.fastjrun.eladmin.itrms.dto.ServerDTO;
import com.fastjrun.eladmin.itrms.dto.ServerQueryDTO;
import com.fastjrun.eladmin.itrms.entity.RmsServer;
import com.fastjrun.eladmin.itrms.entity.RmsServerExample;
import com.fastjrun.eladmin.itrms.mapstruct.RmsServer2ServerVO;
import com.fastjrun.eladmin.itrms.mapstruct.ServerDTO2RmsServer;
import com.fastjrun.eladmin.itrms.service.RmsServerControllerService;
import com.fastjrun.eladmin.itrms.vo.ServerVO;
import com.fastjrun.eladmin.vo.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import me.zhengjie.utils.FileUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service("rmsServerControllerService")
public class RmsServerControllerServiceImpl extends BaseItmsService implements RmsServerControllerService {
    @Resource
    RmsServerDao rmsServerDao;
    @Resource
    RmsServer2ServerVO rmsServer2ServerVO;
    @Resource
    ServerDTO2RmsServer serverDTO2RmsServer;

    private RmsServerExample retrive(ServerQueryDTO requestBody){
        log.debug("requestBody={}",requestBody);
        RmsServerExample rmsServerSelectExample=new RmsServerExample();
        return rmsServerSelectExample;
    }

    @Override
    public void exportServer(HttpServletResponse response, ServerQueryDTO requestBody){
        RmsServerExample rmsServerExample=this.retrive(requestBody);
        List<RmsServer> all = rmsServerDao.selectByExample(rmsServerExample);
        List<Map<String,Object>> list= Lists.newArrayList();
        for (RmsServer server : all) {
            Map<String,Object> map = Maps.newHashMap();
            map.put("服务器编号", server.getServerNo());
            map.put("服务器名称", server.getServerName());
            map.put("服务器类型", server.getServerType());
            map.put("是否分配", server.getStatus());
            map.put("公网IP", server.getPublicIp());
            map.put("内网IP", server.getPrivateIp());
            map.put("操作系统", server.getOs());
            map.put("cpu", server.getCpu());
            map.put("内存：单位G", server.getMemory());
            map.put("磁盘：单位G", server.getDisk());
            map.put("机密信息，只有负责人可操作", server.getSecurityInfo());
            map.put("备注", server.getRemark());
            map.put("负责用户", server.getOwnerUser());
            map.put("创建用户", server.getCreateUser());
            map.put(" createDate",  server.getCreateDate());
            map.put(" updateDate",  server.getUpdateDate());
            list.add(map);
        }
        try {
            FileUtil.downloadExcel(list, response);
        } catch (IOException e) {
            log.warn("",e);
            throw new ServiceException(e);
        }

    }

    @Override
    public PageResult<ServerVO> queryServer(Integer page, Integer size, String sort) {
        if (page == null) {
            page = PAGE_CURRENT;
        }
        if (size == null) {
            size = PAGE_SIZE;
        }

        PageHelper.startPage(page, size, true);

        RmsServerExample rmsServerExample = new RmsServerExample();

        PageInfo<RmsServer> pageInfo = new PageInfo<>(rmsServerDao.selectByExample(rmsServerExample));
        List<ServerVO> list = rmsServer2ServerVO.toEntity(pageInfo.getList());

        PageResult<ServerVO> pageResult = new PageResult<>();
        pageResult.setContent(list);
        pageResult.setTotalElements((int) pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public void createServer(ServerDTO requestBody) {
        RmsServer rmsServer=this.serverDTO2RmsServer.toEntity(requestBody);
        rmsServerDao.insert(rmsServer);

    }

    @Override
    public void updateServer(ServerDTO requestBody) {
        RmsServer rmsServer=this.serverDTO2RmsServer.toEntity(requestBody);
        rmsServerDao.updateByPrimaryKey(rmsServer);

    }

    @Override
    public void deleteServer(Long[] ids) {
        Arrays.stream(ids).forEach(id->rmsServerDao.deleteByPrimaryKey(id));
    }
}
